package com.daatak.customer.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import com.daatak.customer.domain.CustomerAddress;
import com.daatak.customer.domain.CustomerObject;

@Repository
@Transactional
public class CustomerDao implements CustomerDaoI {
	
	//private HibernateTemplate hibernateTemplate;
	@Autowired
	private SessionFactory sessionFactory;
	private TransactionTemplate transactionTemplate;
	
	public void setSessionFactory(SessionFactory sessionFactory) 
	{
		//hibernateTemplate = new HibernateTemplate(sessionFactory);
		this.sessionFactory = sessionFactory;
	}
	
	public void setTransactionTemplate(TransactionTemplate transactionTemplate)
	{
		this.transactionTemplate = transactionTemplate;
	}
	
	public List<CustomerObject> list()
	{
		//return null;//hibernateTemplate.find("from UserView");
		Session session = sessionFactory.getCurrentSession();
		return session.createQuery("from CustomerObject").list();
		
	}
	
	public void create(CustomerObject customerObject)
	{
		
		Session session = sessionFactory.getCurrentSession();
		session.merge(customerObject);
		
		
	}
	
	public CustomerObject findById(Integer Id)
	{
		
		Session session = sessionFactory.getCurrentSession();
		return (CustomerObject)session.get(CustomerObject.class,Id);
		
	}
	
	
	public CustomerObject loadUserByUsername(String arg0)
	{
		// TODO Auto-generated method stub
		/*List<UserObject> userList=hibernateTemplate.find("from UserObject where userName=?",arg0);
		if(userList.size()>0)
			return userList.get(0);
		else*/
			return null;
		
		
	}
	
	public void delete(CustomerObject  customerObject)
	{
		sessionFactory.getCurrentSession().delete(customerObject);
	}
	
	public List<CustomerAddress> geCustomerAddress(Integer customerId)
	{
		//UserObject user = userDao.getUserAddress(userId);
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(CustomerAddress.class);
		criteria.add(Restrictions.eq("referenceId", customerId));
		return criteria.list();
		
	}
	
}
